{% extends 'jwxt/base.html' %}
{% block title %}B站视频提取 - API合集{% endblock %}
{% block content %}
<div class="row">
    <div class="col-md-8 col-lg-6 mx-auto">
        <div class="card">
            <div class="card-header">B站视频提取</div>
            <div class="card-body">
                <div class="mb-3">
                    <label class="form-label">BV号</label>
                    <input id="bvId" type="text" class="form-control" placeholder="例如：BV1xx411c7mD">
                </div>
                <div class="form-check form-switch mb-3">
                    <input class="form-check-input" type="checkbox" id="downloadSwitch">
                    <label class="form-check-label" for="downloadSwitch">下载到服务器（media/bilibili）</label>
                </div>
                <div class="d-grid gap-2">
                    <button class="btn btn-primary" id="btnExtract">提取</button>
                </div>
                <hr>
                <div id="result" class="mt-3" style="white-space: pre-wrap;"></div>
            </div>
        </div>
    </div>
    <script>
    (function(){
        const btn = document.getElementById('btnExtract');
        const bvInput = document.getElementById('bvId');
        const downloadSwitch = document.getElementById('downloadSwitch');
        const result = document.getElementById('result');

        btn.addEventListener('click', async () => {
            const bv = (bvInput.value || '').trim();
            if (!bv || !bv.startsWith('BV')) {
                result.textContent = '请输入正确的BV号（示例：BV1xx411c7mD）';
                return;
            }
            result.textContent = '处理中，请稍候...';
            try {
                const params = new URLSearchParams({ bv_id: bv, download: downloadSwitch.checked ? 'true' : 'false' });
                const resp = await fetch(`/api/bilibili/extract?${params.toString()}`);
                const data = await resp.json();
                if (data.code === 0) {
                    const videoUrl = data?.data?.video_url;
                    if (videoUrl) {
                        // 直接跳转到视频真实链接
                        window.location.href = videoUrl;
                        return;
                    }
                    result.textContent = '未返回视频链接，请重试或检查BV号是否正确。';
                } else {
                    result.textContent = '失败：' + (data.msg || '未知错误');
                }
            } catch (e) {
                result.textContent = '请求异常：' + e;
            }
        });
    })();
    </script>
{% endblock %}


